#include<bits/stdc++.h>
#define gc getchar
#define itn int
#define x first
#define y second
#define eb emplace_back
#define em emplace
#define pb push_back
#define db double
#define y1 yy1_yyds
using namespace std; typedef long long ll; typedef unsigned long long ull;
// https://www.luogu.com.cn/discuss/522581 About "const"
ll read() {
	ll x = 0; short fh = 1; char ch = gc();
	while (!isdigit(ch)) {
		if (ch == '-') fh = -1;
		if (ch < 10) exit(0); 
		ch = gc();
	}
	while (isdigit(ch))
		x = x * 10 + (ch ^ 48), ch = gc();
	return fh * x;
}
#ifndef ONLINE_JUDGE
void debug() {cerr << "\n";}
template<typename Typ1> void debug(Typ1 arg) {cerr << arg << "\n";}
template<typename Typ1, typename ...Typ2> void debug(Typ1 arg, Typ2 ...args) {
	cerr << arg << " ", debug(args...);
}
#else
void debug() {}
template<typename Typ1> void debug(Typ1 arg) {}
template<typename Typ1, typename ...Typ2> void debug(Typ1 arg, Typ2 ...args) {}
#endif
void writeln(ll arg) {printf("%lld\n", arg);}
template<typename ...Typ2> void writeln(ll arg, Typ2 ...args) {
	printf("%lld ", arg), writeln(args...);
}
typedef pair <int, int> pii; typedef pair <ll, ll> pll;
const char Y_E_S[] = "YES", N__O[] = "NO";
// const char Y_E_S[] = "Yes", N__O[] = "No";
// #define infinite_testcase
// #define multiple_testcase
// #define output_Yes_No
const int DUST = 327, N = 514, M = -1;
char str[N];
string s[N]; int v[N];
int n, m;
namespace Sub1 {
	db ans = 0; char best_str[9];
	void dfs(int dph) {
		if(dph == n) {
			db res = 1; int t = 0;
			for(int k = 1; k <= m; k++) {
				for(int i = /*1*/0; i <= n - (int)s[k].length(); i++)//nm 这里我都是按下标从1开始算的
					for(int l = i, j = 0; l < n && j < (int)s[k].length(); l++, j++) {
						if(str[l] != s[k][j]) break;
						if(j == (int)s[k].length() - 1) res *= v[k], t++;
					}
			}
			if(t) res = pow(res, 1.0 / t);
			if(res > ans) ans = res, strcpy(best_str, str);
		/**/return;
		}
		if(str[dph] == '.') {
			for(char i = 48; i < 58; i++)
				str[dph] = i, dfs(dph + 1);
		/**/str[dph] = '.';
		}
		else dfs(dph + 1);
	}
	void major() {
		// debug("ttt");
		dfs(0);
		puts(best_str);
		// debug(ans);
	}
}
// namespace Test6 {
	// char S[N];
// 	
	// int nxt[N];
	// void get_nxt(const char *s, int n) {
		// nxt[1] = 0;
		// for(int i = 2, j = 0; i <= n; i++) {
			// while(j && s[j + 1] != s[i]) j = nxt[j];
			// if(s[j + 1] == s[i]) j++;
			// nxt[i] = j;
		// }
	// }
	// bool comp(const char *s, const char *t, int n) {
		// for(int i = 1, j = 0; t[i]; i++) {
			// while(j && s[j + 1] != t[i]) j = nxt[j];
			// if(s[j + 1] == t[i]) j++;
			// if(j == n) {
				// return 1;
				// j = nxt[j];
			// }
		// }
		// return 0;
	// }
	// void major() {
		// for(int k = 1; k <= m; k++) {
			// strcpy(S, str);
			// get_nxt(s[k].c_str() - 1, s[k].length());
// 			
		// }
	// }
// }
bool major(int Case = 1) {
	n = read(), m = read();
	scanf("%s", str);
	int TS = 0;
	for(int i = 1; i <= m; i++) cin >> s[i], TS += s[i].length(), v[i] = read();
	if(n <= 6 && TS <= 20) return Sub1::major(), 0;
	// return Test6::major(), 1;
	return Case ^= Case ^ Case;
}
void initial_function(int argc, char **argv) {
	**argv = argc; /* <- place_holder
	you won't give up no matter what happens, will you?
	code time: 10:30 >> 12:01
	---
	题意：由题目给定的模板串把 . 换成 0~9 构造字符串，
	使每个在其里出现的字符串的按次数加权的几何平均数最大
	
	唉，9~11 判子串没时间写了，只能写写 6 了
	啊？6 也要 kmp？
	*/
	freopen("magic.in", "r", stdin);
	freopen("magic.out", "w", stdout);
}
signed main(int argc, char **argv) {
	initial_function(argc, argv);
	int Case = 1, Maxcase = 1;
	for (
#ifdef multiple_testcase
		  Maxcase = read()
#endif
				     	  ;
#ifndef infinite_testcase
							Case <= Maxcase
#endif
				     					   ; Case++)
#ifdef output_Yes_No
		puts(major(Case) ? Y_E_S : N__O);
#else
		major(Case);
#endif
	return DUST ^ 0x147;
}
